<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;
use App\Annotation\Api;
use App\Annotation\View;

use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Goodstype;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;


#[Middleware(AdminMiddleware::class)]
#[Controller("admin/goodstype")]
class GoodstypeController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        $name = $this->request->input('name', '');
        if ($name != '') {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $lists = Goodstype::where($where)
            ->orderBy('goods_type_id', 'DESC')
            ->paginate();
        return compact('lists');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $goods_type_id = (int)$this->request->input('goods_type_id', 0);
        $name = (string)$this->request->input('name', '');
        $res = Goodstype::createStore($goods_type_id, [
             'name' => $name
        ]);
        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $goods_type_id = (int)$this->request->input('goods_type_id', 0);
        return ['title' => $goods_type_id > 0 ? '编辑标签' : '新增标签'];
    }


    #[Api]
    #[GetMapping("edit/{goods_type_id}")]
    function editInfo(int $goods_type_id)
    {
        $room_label = Goodstype::where('goods_type_id', $goods_type_id)->select('*')
            ->first() ?: [];
        return compact('room_label');
    }


}
